package com.demo.service.impl;

import com.demo.mapper.ServerMapper;
import com.demo.po.ServerPo;
import com.demo.service.IServerService;
import com.demo.service.common.BizCache;
import com.demo.service.common.BizErrorCode;
import com.demo.service.common.BizException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.sql.SQLException;
import java.util.List;

@Component("serverService")
public class ServerServiceImpl implements IServerService {

    private Logger log = LoggerFactory.getLogger(ServerServiceImpl.class);

    @Qualifier("serverMapper")
    @Autowired
    private ServerMapper serverMapper;

    @Override
    public int countServers(ServerPo cond) {
        log.debug("countServers starting...");
        int count = 0;
        try {
            count = serverMapper.countServers(cond);
        } catch (SQLException ex) {
            log.error("exception:", ex);
            throw new BizException(BizErrorCode.EX_TRANSACTION_FAIL);
        }
        log.debug("countServers end");
        return count;
    }

    @Override
    public List<ServerPo> findServersByPage(ServerPo cond) {
        log.debug("findServersByPage starting...");
        List<ServerPo> servers = null;
        try {
            servers = serverMapper.findServersByPage(cond);
        } catch (SQLException ex) {
            log.error("exception:", ex);
            throw new BizException(BizErrorCode.EX_TRANSACTION_FAIL);
        }
        log.debug("findServersByPage end");
        return servers;
    }

    @Override
    public List<ServerPo> findAllServers(ServerPo cond) {
        log.debug("findAllServers starting...");
        List<ServerPo> servers = null;
        try {
            servers = serverMapper.findAllServers(cond);
        } catch (SQLException ex) {
            log.error("exception:", ex);
            throw new BizException(BizErrorCode.EX_TRANSACTION_FAIL);
        }
        log.debug("findAllServers end");
        return servers;
    }

    @Override
    public ServerPo getServer(Integer serverid) {
        log.debug("getServer starting...");
        ServerPo server = null;
        try {
            server = serverMapper.getServer(serverid);
        } catch (SQLException ex) {
            log.error("exception:", ex);
            throw new BizException(BizErrorCode.EX_TRANSACTION_FAIL);
        }
        log.debug("getServer end");
        return server;
    }

    @Override
    public Integer addServer(ServerPo obj) {
        log.debug("addServer starting...");
        Integer retval = 0;
        try {
            obj.setAddtime(BizCache.getInstance().getNow());
            retval = serverMapper.addServer(obj);

            if (retval == 0) {
                throw new BizException(BizErrorCode.EX_SQL_FAIL);
            }
            retval = obj.getServerid();
        } catch (SQLException ex) {
            log.error("exception:", ex);
            throw new BizException(BizErrorCode.EX_TRANSACTION_FAIL);
        }
        log.debug("addServer end");
        return retval;
    }

    @Override
    public int updateServer(ServerPo obj) {
        log.debug("updateServer starting...");
        int retval = 0;
        try {
            obj.setUpdatetime(BizCache.getInstance().getNow());
            retval = serverMapper.updateServer(obj);
        } catch (SQLException ex) {
            log.error("exception:", ex);
            throw new BizException(BizErrorCode.EX_TRANSACTION_FAIL);
        }
        if (retval == 0) {
            throw new BizException(BizErrorCode.EX_SQL_FAIL);
        }
        log.debug("updateServer end");
        return retval;
    }

    @Override
    public int deleteServer(Integer serverid) {
        log.debug("deleteServer starting...");
        int retval = 0;
        try {
            retval = serverMapper.deleteServer(serverid);
        } catch (SQLException ex) {
            log.error("exception:", ex);
            throw new BizException(BizErrorCode.EX_TRANSACTION_FAIL);
        }
        log.debug("deleteServer end");
        return retval;
    }
}